//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.lemoncommon.util;

import com.example.lemoncommon.exception.ServiceException;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import java.io.IOException;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

public class JsonUtils {
    private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);
    public static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {
    };
    private static ObjectMapper mapper = new ObjectMapper();
    private static ObjectMapper filterMapper;

    public JsonUtils() {
    }

    public static <T> String pojoToJson(T pojo) {
        try {
            return pojo == null ? null : getMapper().writeValueAsString(pojo);
        } catch (IOException var2) {
            throw new ServiceException("Failed to convert Object2JSONString. ", var2.getMessage());
        }
    }

    public static <T> T jsonToPojo(String json, Class<T> pojoClass) {
        try {
            return !StringUtils.hasText(json) ? null : getMapper().readValue(json, pojoClass);
        } catch (IOException var3) {
            throw new ServiceException("Failed to convert JSONString2Object. ", var3.getMessage());
        }
    }

    public static <T> List<T> jsonToPojoList(String json, TypeReference<List<T>> valueTypeRef) {
        try {
            return !StringUtils.hasText(json) ? null : (List)getMapper().readValue(json, valueTypeRef);
        } catch (IOException var3) {
            throw new ServiceException("Failed to convert JSONString2Object. ", var3.getMessage());
        }
    }

    public static <T> T jsonToPojo(Reader src, Class<T> pojoClass) throws IOException {
        return getMapper().readValue(src, pojoClass);
    }

    public static <T> T jsonToPojo(String json, TypeReference<T> valueTypeRef) {
        try {
            return !StringUtils.hasText(json) ? null : getMapper().readValue(json, valueTypeRef);
        } catch (IOException var3) {
            throw new ServiceException("Failed to convert JSONString2Object. ", var3.getMessage());
        }
    }

    public static Map<String, Object> jsonToMap(String json) {
        try {
            return !StringUtils.hasText(json) ? null : (Map)getMapper().readValue(json, MAP_TYPE);
        } catch (IOException var2) {
            throw new ServiceException("Failed to convert JSONString2Map. ", var2.getMessage());
        }
    }

    public static <T> T pojoToPojo(T sourceObject, Class<T> targetType) {
        return sourceObject == null ? null : getMapper().convertValue(sourceObject, targetType);
    }

    public static <T> Map<String, Object> pojoToMap(T pojo) {
        return pojo == null ? null : (Map)getMapper().convertValue(pojo, MAP_TYPE);
    }

    public static <T> Map<String, Object> pojoToMapWithSpecifiedFields(T pojo, String... fields) {
        String json = pojoToJsonWithSpecifiedFields(pojo, fields);
        return jsonToMap(json);
    }

    public static <T> Map<String, Object> pojoToMapWithFilterFields(T pojo, String... fields) {
        String json = pojoToJsonWithFilterFields(pojo, fields);
        return jsonToMap(json);
    }

    public static <T> Map<String, Object> pojoToMapWithSpecifiedFields(T pojo, String filterId, String... fields) {
        String json = pojoToJsonWithSpecifiedFields(pojo, filterId, fields);
        return jsonToMap(json);
    }

    public static <T> Map<String, Object> pojoToMapWithFilterFields(T pojo, String filterId, String... fields) {
        String json = pojoToJsonWithFilterFields(pojo, filterId, fields);
        return jsonToMap(json);
    }

    public static <T> String pojoToJsonWithSpecifiedFields(T pojo, String... fields) {
        return pojo == null ? null : pojoToJsonWithSpecifiedFields(pojo, pojo.getClass().getName(), fields);
    }

    public static <T> String pojoToJsonWithFilterFields(T pojo, String... fields) {
        return pojo == null ? null : pojoToJsonWithFilterFields(pojo, pojo.getClass().getName(), fields);
    }

    public static <T> String pojoToJsonWithSpecifiedFields(T pojo, String filterName, String... fields) {
        if (pojo == null) {
            return null;
        } else {
            if (StringUtils.isEmpty(filterName)) {
                filterName = pojo.getClass().getName();
            }

            return pojoToJson(pojo, filterName, SimpleBeanPropertyFilter.filterOutAllExcept(fields));
        }
    }

    public static <T> String pojoToJsonWithFilterFields(T pojo, String filterName, String... fields) {
        if (pojo == null) {
            return null;
        } else {
            String filter = filterName;
            if (StringUtils.isEmpty(filterName)) {
                filter = pojo.getClass().getName();
            }

            return pojoToJson(pojo, filter, SimpleBeanPropertyFilter.serializeAllExcept(fields));
        }
    }

    public static <T> Map<String, Object> pojoToMap(T pojo, PropertyFilter filter) {
        if (pojo == null) {
            return null;
        } else {
            String json = pojoToJson(pojo, pojo.getClass().getName(), filter);
            return jsonToMap(json);
        }
    }

    public static <T> Map<String, Object> pojoToMap(T pojo, String filterName, PropertyFilter filter) {
        if (pojo == null) {
            return null;
        } else {
            String json = pojoToJson(pojo, filterName, filter);
            return jsonToMap(json);
        }
    }

    public static <T> String pojoToJson(T pojo, PropertyFilter filter) {
        return pojo == null ? null : pojoToJson(pojo, pojo.getClass().getName(), filter);
    }

    public static <T> String pojoToJson(T pojo, String filterName, PropertyFilter filter) {
        if (StringUtils.isEmpty(filterName)) {
            filterName = pojo.getClass().getName();
        }

        SimpleFilterProvider filters = (new SimpleFilterProvider()).addFilter(filterName, filter);

        try {
            String e = getFilterMapper().writer(filters).writeValueAsString(pojo);
            if (log.isDebugEnabled()) {
                log.debug("pojoToJson :" + e);
            }

            return e;
        } catch (IOException var5) {
            throw new ServiceException("Failed to convert Object2JSONString. ", var5.getMessage());
        }
    }

    private static ObjectMapper getMapper() {
        Class var0 = JsonUtils.class;
        synchronized(JsonUtils.class) {
            if (mapper != null) {
                return mapper;
            } else {
                mapper = new ObjectMapper();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                mapper.setDateFormat(simpleDateFormat);
                return mapper;
            }
        }
    }

    private static ObjectMapper getFilterMapper() {
        Class var0 = JsonUtils.class;
        synchronized(JsonUtils.class) {
            if (filterMapper != null) {
                return filterMapper;
            } else {
                filterMapper = new ObjectMapper();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                filterMapper.setDateFormat(simpleDateFormat);
                return filterMapper;
            }
        }
    }

    static {
        mapper.setSerializationInclusion(Include.ALWAYS);
        mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker().withFieldVisibility(Visibility.ANY).withGetterVisibility(Visibility.NONE).withSetterVisibility(Visibility.NONE).withCreatorVisibility(Visibility.NONE));
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);
        mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    }
}
